/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.client.ui;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import org.gwtportlets.portlet.client.DoNotSendToServer;
import org.gwtportlets.portlet.client.WidgetFactory;
import org.gwtportlets.portlet.client.layout.LayoutConstraints;
import org.gwtportlets.portlet.client.ui.LayoutPanel;
import org.gwtportlets.portlet.client.ui.Portlet;
import org.gwtportlets.portlet.client.ui.PortletFactory;
import org.gwtportlets.portlet.client.util.FormBuilder;
import za.org.foobar.famon.frontend.client.BaseCallback;
import za.org.foobar.famon.frontend.client.Famon;
import za.org.foobar.famon.frontend.client.FamonService;
import za.org.foobar.famon.frontend.client.util.ClientUtil;

import java.util.List;

/**
 *
 */
public class PageEditorPortlet  extends Portlet {

    public List<String> pages;

    private ListBox pageListBox = new ListBox();
    private TextArea textArea = new TextArea();

    public PageEditorPortlet() {
        LayoutPanel layout = new LayoutPanel();
        initWidget(layout);

        if (!ClientUtil.checkIsAdmin(layout)) {
            return;
        }
        FormBuilder form = new FormBuilder();
        form
                .label("Page")
                .field(pageListBox);
        Button saveButton = new Button("Save");
        form.field(saveButton);

        Button newButton = new Button("New");
        form.field(newButton);

        layout.add(form.getFormInPanel(), 30);
        layout.add(textArea, LayoutConstraints.AUTO);

        pageListBox.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent changeEvent) {
                loadPage();
            }
        });

        saveButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                savePage();
            }
        });

        newButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                newPage();
            }
        });
    }

    private void restore(Factory f) {
        pages = f.pages;
        pageListBox.clear();
        if (pages != null) {
            for (String page : pages) {
                pageListBox.addItem(page);
            }
        }
        loadPage();
    }

    private void loadPage() {
        if (pageListBox.getSelectedIndex() < 0) {
            return;
        }
        FamonService.App.get().getPageXml(pageListBox.getItemText(pageListBox.getSelectedIndex()),
                new BaseCallback<String>() {
                    public void onSuccess(String result) {
                        textArea.setText(result);
                        Famon.getInstance().hideBusy();
                    }
                });
    }

    private void savePage() {
        if (pageListBox.getSelectedIndex() < 0) {
            return;
        }
        FamonService.App.get().setPageXml(pageListBox.getItemText(pageListBox.getSelectedIndex()),
                textArea.getText(),
                new BaseCallback() {
                    public void onSuccess(Object result) {
                        Famon.getInstance().hideBusy();
                        Window.alert("Page saved");
                        loadPage();
                    }
                });
    }

    private void newPage() {
        String name = Window.prompt("Enter in the page's name", "");
        FamonService.App.get().newPage(name, new BaseCallback());
    }

    public WidgetFactory createWidgetFactory() {
        return new Factory(this);
    }

    public static class Factory extends PortletFactory<PageEditorPortlet> {

        @DoNotSendToServer
        public List<String> pages;

        public Factory() {
        }

        public Factory(PageEditorPortlet p) {
            super(p);
            pages = p.pages;
        }

        public void refresh(PageEditorPortlet p) {
            super.refresh(p);
            p.restore(this);
        }

        public PageEditorPortlet createWidget() {
            return new PageEditorPortlet();
        }
    }

}
